package com.kingwood.algorithm.test;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author 22025812
 * @Description: TODO
 * https://blog.csdn.net/qfc_128220/article/details/134454067
 * 内存冷热标记
 * @Date: 2024/2/27 13:55
 * @Modified By：
 * @Version 1.0
 */
public class DuoTiaoJianSort {

    public static void makerMemory() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        HashMap<Integer, Integer> cnts = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            cnts.put(num, cnts.getOrDefault(num, 0) + 1);
        }

        int threshold = scanner.nextInt();
        cnts.keySet().removeIf(num -> cnts.get(num) < threshold);
//        cnts.keySet().removeIf(num -> cnts.get(num) < threshold);
        int size = cnts.size();
        System.out.println(size);
        if (size == 0) {
            return;
        }

        cnts.entrySet().stream().sorted(
                (a,b) ->
                    a.getValue() - b.getValue() != 0
                            ? b.getValue() - a.getValue()
                            : a.getKey() - b.getKey()
        ).forEach(
                a -> System.out.println(a.getKey())
        );

//        cnts.entrySet().stream().sorted(
//                (a, b) -> a.getValue() - b.getValue() != 0 ? b.getValue() - a.getValue() : a.getKey() - b.getKey()
//        ).forEach(a -> System.out.println(a.getKey()));

        return;
    }


    public static void main(String[] args) {
        makerMemory();

    }
}
